#ifndef _CRT_BARRIER_H_
#define _CRT_BARRIER_H_

#include <crt_sem.h>
#include <crt_error.h>

#ifdef __cplusplus
extern "C" {
#endif

#if (TARGET_OS == OS_WIN)

typedef struct {
    unsigned int n;
    unsigned int count;
    _mutex_t mutex;
    _sem_t turnstile1;
    _sem_t turnstile2;
} _barrier_t;

#elif (TARGET_OS == OS_POSIX)

typedef struct {
    unsigned threshold;
    unsigned in;
    unsigned out;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} _barrier_t;

#elif (TARGET_OS == OS_MACH)

typedef struct { // mach .....no found  ==> myself devlop
    unsigned threshold;
    unsigned in;
    unsigned out;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} _barrier_t;

#elif (TARGET_OS == OS_UNIX)

typedef struct { // unix .....no found  ==> myself devlop
    unsigned threshold;
    unsigned in;
    unsigned out;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} _barrier_t;

#endif

int uv_barrier_init(_barrier_t* barrier, unsigned int count);
int uv_barrier_destroy(_barrier_t* barrier);
int uv_barrier_wait(_barrier_t* barrier);

#ifdef __cplusplus
}
#endif

#endif
